

package com.maoshi.shop.user.platform.controller;

import com.maoshi.shop.common.annotation.SysLog;
import com.maoshi.shop.user.comment.dto.UserLevelDto;
import com.maoshi.shop.user.comment.model.UserLevel;
import com.maoshi.shop.user.comment.service.UserLevelService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 会员等级
 *
 * @author LGH
 * @date 2020-02-26 16:03:14
 */
@RestController
@AllArgsConstructor
@RequestMapping("/user/userLevel" )
public class UserLevelController {

    private final UserLevelService userLevelService;


    /**
     * 用户等级列表
     */
    @GetMapping("/list" )
    public ResponseEntity<List<UserLevelDto>> listUserLevel(Integer userLevelType) {
        return ResponseEntity.ok(userLevelService.listUserLevelsByUserLevelType(userLevelType));
    }


    /**
     * 通过id查询会员等级
     * @param id id
     * @return 单个数据
     */
    @GetMapping("/info/{id}" )
    public ResponseEntity<UserLevel> getById(@PathVariable("id") Long id) {
        UserLevel userLevel = userLevelService.getUserLevelById(id);
        return ResponseEntity.ok(userLevel);
    }

    /**
     * 新增会员等级
     * @param userLevel 会员等级
     * @return 是否新增成功
     */
    @SysLog("新增会员等级" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('user:userLevel:save')" )
    public ResponseEntity<Boolean> save(@RequestBody @Valid UserLevel userLevel) {
        return ResponseEntity.ok(userLevelService.save(userLevel));
    }

    /**
     * 修改会员等级
     * @param userLevel 会员等级
     * @return 是否修改成功
     */
    @SysLog("修改会员等级" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('user:userLevel:update')" )
    public ResponseEntity<Void> updateById(@RequestBody @Valid UserLevel userLevel) {
        userLevelService.updateUserLevelList(userLevel);
        return ResponseEntity.ok().build();
    }

    /**
     * 通过id删除会员等级
     * @param userLevel 会员等级
     * @return 是否删除成功
     */
    @SysLog("删除会员等级" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('user:userLevel:delete')" )
    public ResponseEntity<Boolean> removeById(@RequestBody @Valid UserLevel userLevel) {
        return ResponseEntity.ok(userLevelService.deleteUserLevel(userLevel));
    }

    /**
     * 更新用户会员等级
     * @return 是否修改成功
     */
    @PutMapping("/updateUserLevel")
    public ResponseEntity<Boolean> updateUserLevel(@RequestBody @Valid UserLevel userLevel) {
        return ResponseEntity.ok(userLevelService.updateUserLevel(userLevel));
    }

}
